const { Controller } = require("egg");
const fs = require("fs");

const PREFIX = "article/assets/";

class CosController extends Controller {
  async deleteAssets() {
    const { ctx, service } = this;
    const { url } = ctx.request.body;
    let [, fileKey] = url.split(PREFIX);
    fileKey = PREFIX + fileKey || url;
    const data = await service.cos.delete(fileKey);
    if (data) {
      this.ctx.helper.success(data);
    } else {
      this.ctx.helper.error("删除失败, 请重试");
    }
  }

  async sts() {
    const data = await this.service.cos.generateTemporaryCredentials();
    if (data) {
      this.ctx.helper.success(data);
    } else {
      this.ctx.helper.error("生成腾讯云key失败");
    } 
  }
}

module.exports = CosController;
